# encoding:utf-8
'''
@author:  adog
@Email   : 
Created on 2021-03-27 12:57:08
'''
import apps.adog.util.dbcom as dbcom
import util.mdbcom as mdbcom
import util.xtool as xtool
import pandas as pd


class DataMan():
    
    def __init__(self, startDate="2015-01-01",endDate=xtool.nowDate(),is15minToday=False,obs=[],freq='1d', field=[],db='mongo',code_type='fin8'):
        sql=''
        self.data=None
        if db=='mongo':
            if is15minToday:
                self.data=mdbcom.find('jq_hq_15m_today',
                               queryDict={'code_type':code_type,'time':{'$lte':xtool.nowOnlyTimeMins()}} , 
                               outType='pandas')
                
            else:
                self.data=mdbcom.find('jq_hq_%s'%(freq),
                               queryDict={'code_type':code_type,'$and':[{'date':{'$gte': startDate}},{'date':{'$lte':endDate}}]} , 
                               outType='pandas')
                
            if len(obs)>0:
                self.data=self.data.loc[self.data['cod'].isin(obs)]
                
            #self.future_info=mdbcom.find('future_info',sortListOfTuple=[('_id',1)],outType='pandas')
            
        else:
            if len(obs)==0:
                sql='SELECT * FROM future.jq_hq_%s where code_type="%s" and date >="%s" and date<="%s" order by cod'%(freq,code_type,startDate,endDate) # and (cod="L" or cod="PP" or cod="V") 
            else:
                sql='SELECT * FROM future.jq_hq_%s where code_type="%s" and date >="%s" and date<="%s" cod in ("%s") order by cod'%(freq,code_type,startDate,endDate,'","'.join(obs))
            d=dbcom.select_tuple_dict(sql)
            
            self.data=pd.DataFrame.from_records(d)
            
        
        fi=dbcom.select_tuple_dict('select *, 同列 as "blk_cn" from future_info order by _id')
        self.future_info=pd.DataFrame.from_records(fi)
        
        
        if len(field)>0:
            self.data=self.data[field]
        
        
        

if __name__=='__main__':
    sql='select * from future.future_info order by _id'
    fi=dbcom.select_tuple_dict(sql)
    #fi_df=pd.DataFrame.from_records(fi)
    mdbcom.saveBatch('future_info', fi,)
    print('furtue_info migrated to mongo')